package com.fri.idcread;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.yachtar.property.R;
import com.yachtar.property.idcard.IDCard;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

@SuppressWarnings("unused")
public class idcread {
	/** Called when the activity is first created. */
	String s;
	private String databasepath = "/storage/sdcard0/";
	private String databasefn = "license.dat";
	private String filepath = "/storage/sdcard0/zp.bmp";
	private static final String TAG = "com/fri/idcread";
	private String shuju;
//	private Stm32Manager Stm32S;
	private Context mContext;

	public void init(Context context) throws IOException {
		this.mContext = context;
		copyDatabase(context);
		if (InitComm("/dev/ttyMT1") == 1) {
		} else {
			throw new IOException("open ck f");
		}
	}

	File file = new File(filepath);
	File file1 = new File("/storage/sdcard0/wzuni.txt");

	public IDCard duibi() {
		IDCard idC = null;
		if (Authenticate() == 1)
		{
			if (ReadContent("/storage/sdcard0/") == 1)
			{
				idC = new IDCard();
				if (file.exists()) {
					Bitmap bm = BitmapFactory.decodeFile(filepath);
					idC.setHeadBitmap(bm);
				}

				try {
					handleData(idC);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		return idC;
	}

	public native int InitComm(String port);

	public native int CloseComm();

	public native int ReadContent(String path);

	public native int Authenticate();

	static {
		System.loadLibrary("idcread");
	}

	public void close() {
		// TODO Auto-generated method stub
		CloseComm();
	}

	public void handleData(IDCard idC) throws Exception {
		FileInputStream in = new FileInputStream(file1);
		FileChannel channel = in.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate((int) file1.length());
		channel.read(buffer);
		getRestore(buffer, idC);
		in.close();
	}

	public void getRestore(ByteBuffer oldBuf, IDCard idC) throws Exception {
		oldBuf.rewind();
		int size = oldBuf.capacity();
		ByteBuffer reBuffer = ByteBuffer.allocate(size + 2);
		byte[] heard = new byte[] { (byte) 0xFE, (byte) 0xEF };
		reBuffer.put(heard);
		byte[] oldbyte = new byte[2];
		while (oldBuf.position() < size) {
			oldBuf.get(oldbyte);
			reBuffer.put(getswap(oldbyte));
		}
		shuju = new String(reBuffer.array(), "utf-16");
		chuli(idC);
	}

	private byte[] getswap(byte[] bb) {
		byte[] returnb = new byte[2];
		returnb[0] = bb[1];
		returnb[1] = bb[0];
		return returnb;
	}

	public void chuli(IDCard idc) {
		int a = shuju.length();
		char[] temp = shuju.toCharArray();
		StringBuffer name = new StringBuffer();
		String xinbie = new String();
		StringBuffer minzu1 = new StringBuffer();
		String minzu = null;
		StringBuffer lian = new StringBuffer();
		StringBuffer yue = new StringBuffer();
		StringBuffer ri = new StringBuffer();
		StringBuffer zhuzhi = new StringBuffer();
		StringBuffer id = new StringBuffer();
		StringBuffer qianfa = new StringBuffer();
		StringBuffer youxiao = new StringBuffer();
		for (int i = 0; i < a; i++) {
			System.out.println(i + "--" + temp[i]);
			if (i > 0 && i < 15) {
				name.append(temp[i]);
			}
			if (i == 16) {
				if (temp[i] == '1') {
					xinbie = "男";
				}
				if (temp[i] == '2') {
					xinbie = "女";
				}
			}
			if (i >= 17 && i <= 18) {
				minzu1.append(temp[i]);
				if (i == 18) {
					String minzu2 = minzu1.toString();
					if (minzu2.equals("01")) {
						minzu = "��";
					} else if (minzu2.equals("02")) {
						minzu = "�ɹ�";
					} else if (minzu2.equals("03")) {
						minzu = "��";
					} else if (minzu2.equals("04")) {
						minzu = "��";
					} else if (minzu2.equals("05")) {
						minzu = "ά���";
					} else if (minzu2.equals("06")) {
						minzu = "��";
					} else if (minzu2.equals("07")) {
						minzu = "��";
					} else if (minzu2.equals("08")) {
						minzu = "׳";
					} else if (minzu2.equals("09")) {
						minzu = "����";
					} else if (minzu2.equals("10")) {
						minzu = "����";
					} else if (minzu2.equals("11")) {
						minzu = "��";
					} else if (minzu2.equals("12")) {
						minzu = "��";
					} else if (minzu2.equals("13")) {
						minzu = "��";
					} else if (minzu2.equals("14")) {
						minzu = "��";
					} else if (minzu2.equals("15")) {
						minzu = "����";
					} else if (minzu2.equals("16")) {
						minzu = "����";
					} else if (minzu2.equals("17")) {
						minzu = "������";
					} else if (minzu2.equals("18")) {
						minzu = "��";
					} else if (minzu2.equals("19")) {
						minzu = "��";
					} else if (minzu2.equals("20")) {
						minzu = "����";
					} else if (minzu2.equals("21")) {
						minzu = "��";
					} else if (minzu2.equals("22")) {
						minzu = "�";
					} else if (minzu2.equals("23")) {
						minzu = "��ɽ";
					} else if (minzu2.equals("24")) {
						minzu = "����";
					} else if (minzu2.equals("25")) {
						minzu = "ˮ";
					} else if (minzu2.equals("26")) {
						minzu = "����";
					} else if (minzu2.equals("27")) {
						minzu = "����";
					} else if (minzu2.equals("28")) {
						minzu = "����";
					} else if (minzu2.equals("29")) {
						minzu = "�¶�����";
					} else if (minzu2.equals("30")) {
						minzu = "��";
					} else if (minzu2.equals("31")) {
						minzu = "���Ӷ�";
					} else if (minzu2.equals("32")) {
						minzu = "����";
					} else if (minzu2.equals("33")) {
						minzu = "Ǽ";
					} else if (minzu2.equals("34")) {
						minzu = "����";
					} else if (minzu2.equals("35")) {
						minzu = "����";
					} else if (minzu2.equals("36")) {
						minzu = "ë��";
					} else if (minzu2.equals("37")) {
						minzu = "����";
					} else if (minzu2.equals("38")) {
						minzu = "����";
					} else if (minzu2.equals("39")) {
						minzu = "����";
					} else if (minzu2.equals("40")) {
						minzu = "����";
					} else if (minzu2.equals("41")) {
						minzu = "������";
					} else if (minzu2.equals("42")) {
						minzu = "ŭ";
					} else if (minzu2.equals("43")) {
						minzu = "���α��";
					} else if (minzu2.equals("44")) {
						minzu = "����˹";
					} else if (minzu2.equals("45")) {
						minzu = "���¿�";
					} else if (minzu2.equals("46")) {
						minzu = "����";
					} else if (minzu2.equals("47")) {
						minzu = "����";
					} else if (minzu2.equals("48")) {
						minzu = "ԣ��";
					} else if (minzu2.equals("49")) {
						minzu = "��";
					} else if (minzu2.equals("50")) {
						minzu = "������";
					} else if (minzu2.equals("51")) {
						minzu = "����";
					} else if (minzu2.equals("52")) {
						minzu = "���״�";
					} else if (minzu2.equals("53")) {
						minzu = "����";
					} else if (minzu2.equals("54")) {
						minzu = "�Ű�";
					} else if (minzu2.equals("55")) {
						minzu = "���";
					} else if (minzu2.equals("56")) {
						minzu = "��ŵ";
					} else if (minzu2.equals("57")) {
						minzu = "����";
					} else if (minzu2.equals("58")) {
						minzu = "���Ѫͳ";
					}

				}
			}
			if (i >= 19 && i <= 22) {
				lian.append(temp[i]);
			}
			if (i >= 23 && i <= 24) {
				yue.append(temp[i]);
			}
			if (i >= 25 && i <= 26) {
				ri.append(temp[i]);
			}
			if (i >= 27 && i <= 61) {
				zhuzhi.append(temp[i]);
			}
			if (i >= 62 && i <= 79) {
				id.append(temp[i]);
			}
			if (i >= 80 && i <= 94) {
				qianfa.append(temp[i]);
			}
			if (i >= 95 && i <= 110) {
				if (i == 98 || i == 100 || i == 106 || i == 108) {
					youxiao.append(temp[i] + ".");
				} else if (i == 102) {
					youxiao.append(temp[i] + "-");
				} else {
					youxiao.append(temp[i]);
				}
			}

		}
		if (idc != null) {

			idc.setAddress(zhuzhi.toString());
			idc.setBirthday(lian.toString() + "-" + yue.toString() + "-"
					+ ri.toString());
			idc.setGrantDept(qianfa.toString());
			idc.setIDCardNo(id.toString());
			idc.setName(name.toString());
			idc.setSex(xinbie.toString());
			idc.setAvailableDate(youxiao.toString());
			idc.setNation(minzu.toString());
		}
	}

	// �����ݿ⸴�Ƶ�ָ��Ŀ¼
	private void copyDatabase(Context context) {
		try {
			// ���li.db ����·��
			String DATABASEFN = databasepath + databasefn;
			File dir = new File(databasepath);

			// ���/sdcard/liĿ¼�д��ڣ��������Ŀ¼
			if (!dir.exists()) {
				// System.out.print("1");
				dir.mkdir();
			}
			// dir.mkdir();

			// �����Ŀ¼�в�����
			// �ļ������res\rawĿ¼�и�������ļ���
			if (!(new File(DATABASEFN).exists())) {
				InputStream is = context.getResources().openRawResource(
						R.raw.license);
				// System.out.println("2");
				FileOutputStream fos = new FileOutputStream(DATABASEFN);
				// System.out.println("2.1");
				// ������һ�δ��벻�Ǻ���⣬�д��о�
				int length = is.available();
				byte[] buffer = new byte[length]; // 8192
				// System.out.println("3");
				int count = 0;
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
					// System.out.println("4");
				}
				fos.close();
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}